<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="商家"></el-input>
    </el-form-item>
    <el-form-item label="房型" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="房型"></el-input>
    </el-form-item>
    <el-form-item label="单号id" prop="singleId">
      <el-input v-model="dataForm.singleId" placeholder="单号id"></el-input>
    </el-form-item>
    <el-form-item label="同住人id" prop="peopleId">
      <el-input v-model="dataForm.peopleId" placeholder="同住人id"></el-input>
    </el-form-item>
    <el-form-item label="房间id" prop="floorRoomId">
      <el-input v-model="dataForm.floorRoomId" placeholder="房间id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="房价JSON" prop="priceSign">
      <el-input v-model="dataForm.priceSign" placeholder="房价JSON"></el-input>
    </el-form-item>
    <el-form-item label="类型（0未入住1已入住）" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型（0未入住1已入住）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          roomId: '',
          singleId: '',
          peopleId: '',
          floorRoomId: '',
          createTime: '',
          updateTime: '',
          priceSign: '',
          type: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '商家不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '房型不能为空', trigger: 'blur' }
          ],
          singleId: [
            { required: true, message: '单号id不能为空', trigger: 'blur' }
          ],
          peopleId: [
            { required: true, message: '同住人id不能为空', trigger: 'blur' }
          ],
          floorRoomId: [
            { required: true, message: '房间id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          priceSign: [
            { required: true, message: '房价JSON不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型（0未入住1已入住）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/roomReservation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tHotelFloorRoomSingleReserve.sellerId
                this.dataForm.roomId = data.tHotelFloorRoomSingleReserve.roomId
                this.dataForm.singleId = data.tHotelFloorRoomSingleReserve.singleId
                this.dataForm.peopleId = data.tHotelFloorRoomSingleReserve.peopleId
                this.dataForm.floorRoomId = data.tHotelFloorRoomSingleReserve.floorRoomId
                this.dataForm.createTime = data.tHotelFloorRoomSingleReserve.createTime
                this.dataForm.updateTime = data.tHotelFloorRoomSingleReserve.updateTime
                this.dataForm.priceSign = data.tHotelFloorRoomSingleReserve.priceSign
                this.dataForm.type = data.tHotelFloorRoomSingleReserve.type
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/roomReservation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'roomId': this.dataForm.roomId,
                'singleId': this.dataForm.singleId,
                'peopleId': this.dataForm.peopleId,
                'floorRoomId': this.dataForm.floorRoomId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'priceSign': this.dataForm.priceSign,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
